package com.edu.exam.entity.curriculum.questionbank;

import com.edu.exam.enums.curriculum.TagTypeStatus;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;

/**
 * 标签类型实体类
 * 对应数据库表：tag_types
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@Comment("标签类型表")
@SQLDelete(sql = "UPDATE tag_types SET is_deleted = true, delete_time = CURRENT_TIMESTAMP WHERE id = ?")
@Where(clause = "is_deleted = false")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class TagType {

    /**
     * 标签类型唯一标识符
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 标签类型名称
     */
    @NotBlank(message = "标签类型名称不能为空")
    @Size(max = 50, message = "标签类型名称长度不能超过50个字符")
    @Column(name = "tag_type_name", nullable = false, length = 50, unique = true)
    private String tagTypeName;

    /**
     * 标签类型描述
     */
    @Size(max = 200, message = "标签类型描述长度不能超过200个字符")
    @Column(name = "tag_type_description", length = 200)
    private String tagTypeDescription;

    /**
     * 颜色方案
     */
    @Size(max = 100, message = "颜色方案长度不能超过100个字符")
    @Column(name = "color_scheme", length = 100)
    private String colorScheme;

    /**
     * 最大关联数量
     */
    @NotNull(message = "最大关联数量不能为空")
    @Column(name = "max_associations", nullable = false)
    @Builder.Default
    private Integer maxAssociations = 10;

    /**
     * 标签类型状态
     */
    @NotNull(message = "标签类型状态不能为空")
    @Enumerated(EnumType.STRING)
    @Column(name = "tag_type_status", nullable = false, length = 20)
    private TagTypeStatus tagTypeStatus;

    /**
     * 是否系统类型
     */
    @Column(name = "is_system_type")
    @Builder.Default
    private Boolean isSystemType = false;

    /**
     * 排序顺序
     */
    @Column(name = "sort_order")
    @Builder.Default
    private Integer sortOrder = 0;

    /**
     * 标签类型图标
     */
    @Size(max = 50, message = "标签类型图标长度不能超过50个字符")
    @Column(name = "icon_name", length = 50)
    private String iconName;

    /**
     * 使用说明
     */
    @Size(max = 1000, message = "使用说明长度不能超过1000个字符")
    @Column(name = "usage_instructions", columnDefinition = "TEXT")
    private String usageInstructions;

    /**
     * 标签类型元数据，JSON格式
     */
    @Column(name = "metadata", columnDefinition = "TEXT")
    private String metadata;

    /**
     * 记录创建时间
     */
    @Column(name = "created_time", nullable = false, updatable = false)
    private LocalDateTime createdTime;

    /**
     * 记录最后更新时间
     */
    @Column(name = "updated_time", nullable = false)
    private LocalDateTime updatedTime;

    /**
     * 创建人用户ID
     */
    @Size(max = 50, message = "创建人长度不能超过50个字符")
    @Column(name = "created_by", nullable = false, length = 50)
    private String createdBy;

    /**
     * 最后更新人用户ID
     */
    @Size(max = 50, message = "更新人长度不能超过50个字符")
    @Column(name = "updated_by", length = 50)
    private String updatedBy;

    /**
     * 乐观锁版本号
     */
    @Version
    @Column(name = "version", nullable = false)
    @Builder.Default
    private Integer version = 1;

    /**
     * 软删除标记
     */
    @Column(name = "is_deleted", nullable = false)
    @Builder.Default
    private Boolean isDeleted = false;

    /**
     * 删除时间
     */
    @Column(name = "delete_time")
    private LocalDateTime deleteTime;

    @PrePersist
    protected void onCreate() {
        LocalDateTime now = LocalDateTime.now();
        this.createdTime = now;
        this.updatedTime = now;
    }

    @PreUpdate
    protected void onUpdate() {
        this.updatedTime = LocalDateTime.now();
    }
}